package com.notifier;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class StaticExecutor {

	/**
	 * @param args
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		
		AppContext ctx = new AppContext();
		
		
		File xmlFile = new File("WEB-INF/surlyConfig.xml");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		try{
			Document doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("emailAddress");
			
			for(int i=0; i< nList.getLength(); i++){
				Node nNode = nList.item(i);
				if(nNode.getNodeType() == Node.ELEMENT_NODE){
					Element eElement = (Element) nNode;
					ctx = new AppContext(eElement.getAttribute("username"), eElement.getAttribute("password"));
				}
					
			}
			
			if(ctx.username != null && ctx.username != ""){
				Thread t = new Thread(new ThreadRunner(ctx));
				t.start();
			}
			
			System.out.println("Application has started");
			
		}
		catch(FileNotFoundException e){
			System.out.println("surlyConfig.xml not found in any known paths - please create it from the template");
		}
		
		
	}

	

}

